//
//  ViewController.m
//  StudentAddressbook
//
//  Created by 苗钧豪 on 16/7/25.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "addViewController.h"
#import "studentViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,AddStudent>
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)UITableView *tableview;
@end

@implementation ViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    Student *student = [[Student alloc]init];
    if (self.name != nil) {
        student.name = self.name;
        student.age = self.age;
        student.gender = self.gender;
        student.phoneNumber = self.phoneNumber;
        [self.array addObject:student];
    }
    [_tableview reloadData];
    self.name = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [[NSMutableArray alloc]initWithCapacity:0];
    [self getData];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addStudent)];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:(UITableViewStylePlain)];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"soga"];
    [self.view addSubview:_tableview];
}
#pragma mark ================ 数据解析 ================
- (void)getData {
    NSString *filepath = [[NSBundle mainBundle]pathForResource:@"DLS160710(2)" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:filepath];
    for (NSDictionary *dic in arr) {
        Student *student = [[Student alloc]init];
        [student setValuesForKeysWithDictionary:dic];
        [self.array addObject:student];
    }
    
}
#pragma mark ================ 点击方法 ================
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    studentViewController *svc = [[studentViewController alloc]init];
    svc.student = self.array[indexPath.row];
    [self.navigationController pushViewController:svc animated:YES];
}
#pragma mark ================ 添加学生 ================
- (void)addStudent {
    addViewController *avc = [[addViewController alloc]init];
    avc.delegate = self;
    [self.navigationController pushViewController:avc animated:YES];
}
#pragma mark ================ 行数 ================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
#pragma mark ================ cell ================
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"soga"];
    Student *student = _array[indexPath.row];
    cell.textLabel.text = student.name;
    return cell;
    
}
#pragma mark ================ 协议方法 ================
- (void)addStudent:(NSString *)name age:(NSInteger)age gender:(NSString *)gender phoneNUmber:(NSString *)phoneNumber
{
    self.name = name;
    self.age = age;
    self.gender = gender;
    self.phoneNumber = phoneNumber;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
